package com.example.bilibili.func;

import com.example.bilibili.entity.Breply;
import com.example.bilibili.entity.Buser;
import com.example.bilibili.entity.Bvideo;
import com.example.bilibili.service.BreplyService;
import com.example.bilibili.service.BuserService;
import com.example.bilibili.service.BvideoService;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class GrabStarter {

    static CloseableHttpClient client = HttpClientBuilder.create().build();
    static Gson gson = new Gson();
    @Resource
    BvideoService bvideoService;
    @Resource
    BreplyService breplyService;
    @Resource
    BuserService buserService;

    @GetMapping("/insertVideoList")
    public void insertVideoList() throws Exception {
        for (Map.Entry<String, String> entry : BilibiliLinks.VIDEOS.entrySet()) {
            VResult vResult = getEntity(entry.getValue(), VResult.class);

            for (BVideo item : vResult.getData().getList()) {
                Bvideo bvideo = VResult.trans(item, entry.getKey());
                bvideoService.insert(bvideo);
                insertUser(bvideo.getUid());
                insertReply(bvideo.getId());
            }
            System.err.println(entry.getKey() + "分区数据抓取完成");
            Thread.sleep(1000);
        }
        System.err.println("================抓取完成================");
    }

    void insertReply(String vid) throws Exception {
        int count = 5;
        //循环每个video
        for (int i = 0; i < count; i++) {
            //取5次20条评论
            String url = String.format(BilibiliLinks.REPLY, i, vid);
            RResult rResult = null;
            try {
                rResult = getEntity(url, RResult.class);
            } catch (IOException e) {
                e.printStackTrace();
                Thread.sleep(1000);
                rResult = getEntity(url, RResult.class);
            } finally {
                List<Breply> list = RResult.trans(rResult);
                breplyService.insertList(list);
                for (Breply breply : list) {
                    insertUser(breply.getUid());
                    Thread.sleep(1000);
                }
            }
            Thread.sleep(1000);
        }
        System.err.println(vid + "视频评论数据数据抓取完成");
    }

    private void insertUser(String uid) throws IOException {
        String info = String.format(BilibiliLinks.INFO, uid);
        String follow = String.format(BilibiliLinks.FOLLOWER, uid);

        Buser b = UResult.trans(getEntity(info, UResult.class));
        UResult.setFollow(b, getEntity(follow, UResult.class));

        buserService.insert(b);
    }

    public static <T> T getEntity(String url, Class<T> tClass) throws IOException {
        HttpGet httpGet = new HttpGet(url);
        HttpEntity entity = client.execute(httpGet).getEntity();
        return gson.fromJson(EntityUtils.toString(entity), tClass);
    }
}
